﻿using System;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;

using Job.Core;
using Job.Core.Data;
using Job.Core.Module;

namespace Job.ManageMember
{
    /// <summary>
    /// Member模块SQLite实现类。
    /// </summary>
    public class SQLiteMemberRepository : SqlMemberRepository
    {

        #region Role

        protected override Role GetRole(DataReader dr)
        {
            return new Role
            {
                KeyId = (Int32)dr.GetValue<long>("RoleID"),
                RoleName = dr.GetValue<String>("RoleName"),
                IsSys = dr.GetValue<Boolean>("IsSys"),
                Description = dr.GetValue<String>("Description")
            };
        }

        /// <summary>
        /// 添加Role。
        /// </summary>
        public override bool CreateRole(Role role)
        {
            DbConnection conn = DbHelper.CreateConnection();
            DbCommand cmd = DbHelper.CreateCommand();
            cmd.Connection = conn;
            cmd.CommandText = string.Format("INSERT INTO {0}Role(RoleName,IsSys,Description) VALUES(@RoleName,@IsSys,@Description);SELECT LAST_INSERT_ROWId();", TablePrefix);
            cmd.Parameters.Add(DbHelper.NewParam("@RoleName", DbType.String, 15, role.RoleName));
            cmd.Parameters.Add(DbHelper.NewParam("@IsSys", DbType.Boolean, role.IsSys));
            cmd.Parameters.Add(DbHelper.NewParam("@Description", DbType.String, 255, role.Description));

            role.KeyId = Convert.ToInt32(cmd.ExecuteScalar());
            cmd.Parameters.Clear();

            bool result = role.KeyId != default(Int32);
            if (result) RoleCache.Add(role);
            return result;
        }

        #endregion

        #region User

        protected override User GetUser(DataReader dr)
        {
            return new User
            {
                KeyId = (Int32)dr.GetValue<long>("UserID"),
                UserName = dr.GetValue<String>("UserName"),
                Password = dr.GetValue<String>("Password"),
                RoleID = dr.GetValue<Int32>("RoleID"),
                Status = dr.GetValue<UserStatus>("Status"),
                Email = dr.GetValue<String>("Email"),
                CreateTime = dr.GetValue<DateTime>("CreateTime"),
                LoginCount = dr.GetValue<Int32>("LoginCount"),
                LoginError = dr.GetValue<Int32>("LoginError"),
                LoginErrorTime = dr.GetValue<DateTime?>("LoginErrorTime"),
                LoginErrorIP = dr.GetValue<String>("LoginErrorIP"),
                CurrentLoginTime = dr.GetValue<DateTime?>("CurrentLoginTime"),
                CurrentLoginIP = dr.GetValue<String>("CurrentLoginIP"),
                LastLoginTime = dr.GetValue<DateTime?>("LastLoginTime"),
                LastLoginIP = dr.GetValue<String>("LastLoginIP")
            };
        }

        /// <summary>
        /// 添加User。
        /// </summary>
        public override bool CreateUser(User user)
        {
            DbConnection conn = DbHelper.CreateConnection();
            DbCommand cmd = DbHelper.CreateCommand();
            cmd.Connection = conn;
            cmd.CommandText = string.Format("INSERT INTO {0}User(UserName,Password,RoleID,Status,Email,CreateTime,LoginCount,LoginError,LoginErrorTime,LoginErrorIP,CurrentLoginTime,CurrentLoginIP,LastLoginTime,LastLoginIP) VALUES(@UserName,@Password,@RoleID,@Status,@Email,@CreateTime,@LoginCount,@LoginError,@LoginErrorTime,@LoginErrorIP,@CurrentLoginTime,@CurrentLoginIP,@LastLoginTime,@LastLoginIP);SELECT LAST_INSERT_ROWId();", TablePrefix);
            cmd.Parameters.Add(DbHelper.NewParam("@UserName", DbType.String, 15, user.UserName));
            cmd.Parameters.Add(DbHelper.NewParam("@Password", DbType.String, 127, user.Password));
            cmd.Parameters.Add(DbHelper.NewParam("@RoleID", DbType.Int32, user.RoleID));
            cmd.Parameters.Add(DbHelper.NewParam("@Status", DbType.Int32, user.Status));
            cmd.Parameters.Add(DbHelper.NewParam("@Email", DbType.String, 63, user.Email));
            cmd.Parameters.Add(DbHelper.NewParam("@CreateTime", DbType.Date, user.CreateTime));
            cmd.Parameters.Add(DbHelper.NewParam("@LoginCount", DbType.Int32, user.LoginCount));
            cmd.Parameters.Add(DbHelper.NewParam("@LoginError", DbType.Int32, user.LoginError));
            cmd.Parameters.Add(DbHelper.NewParam("@LoginErrorTime", DbType.Date, user.LoginErrorTime));
            cmd.Parameters.Add(DbHelper.NewParam("@LoginErrorIP", DbType.String, 15, user.LoginErrorIP));
            cmd.Parameters.Add(DbHelper.NewParam("@CurrentLoginTime", DbType.Date, user.CurrentLoginTime));
            cmd.Parameters.Add(DbHelper.NewParam("@CurrentLoginIP", DbType.String, 15, user.CurrentLoginIP));
            cmd.Parameters.Add(DbHelper.NewParam("@LastLoginTime", DbType.Date, user.LastLoginTime));
            cmd.Parameters.Add(DbHelper.NewParam("@LastLoginIP", DbType.String, 15, user.LastLoginIP));

            user.KeyId = Convert.ToInt32(cmd.ExecuteScalar());
            cmd.Parameters.Clear();

            bool result = user.KeyId != default(Int32);
            if (result) UserCache.Add(user);
            return result;
        }

        #endregion

    }
}
